//
//  LGCategoryController.m
//  MindBox
//
//  Created by MS on 15-12-31.
//  Copyright (c) 2015年 Lemon. All rights reserved.
//

#define DurTime 0.3
#import "LGCategoryController.h"
#import "GoodsListController.h"
#import "GoodsController.h"
#import "CategoryPageTool.h"
#import "Category_Name_Model.h"
#import "Category_Goods_Model.h"
#import "CategoryView.h"
#import "Category_LabelView.h"
@interface LGCategoryController ()<CategoryViewDelegate,Category_LabelViewDelegate>
@property (nonatomic,weak) UIView * mainView;
@property (nonatomic,weak) UISegmentedControl * segement;
@property (nonatomic,strong) NSArray * category_NameArray;
@property (nonatomic,strong) NSArray * category_GoodsArray;
@property (nonatomic,weak) CategoryView * categoryView;
@property (nonatomic,weak) Category_LabelView * categoryLabelView;
@end

@implementation LGCategoryController

-(CategoryView *)categoryView
{
    if (_categoryView==nil) {
        CategoryView * view = [[CategoryView alloc] initWithFrame:CGRectMake(0,0, WIDTH, HIGHT-49)];
        _categoryView = view;
        _categoryView.delegate = self;
        [self.mainView addSubview:view];
    }
    return _categoryView;
}

-(Category_LabelView *)categoryLabelView
{
    if (_categoryLabelView==nil) {
        Category_LabelView * view = [[Category_LabelView alloc] initWithFrame:CGRectMake(WIDTH, 0, WIDTH, HIGHT-49)];
        _categoryLabelView = view;
        _categoryLabelView.delegate = self;
        [self.mainView addSubview:view];
    }
    return _categoryLabelView;
}

-(NSArray *)category_NameArray
{
    if (_category_NameArray==nil) {
        _category_NameArray =[[NSArray alloc] init];
    }
    return _category_NameArray;
}
-(NSArray *)category_GoodsArray
{
    if (_category_GoodsArray==nil) {
        _category_GoodsArray = [[NSArray alloc] init];
    }
    return _category_GoodsArray;
} 

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor =[UIColor whiteColor];
    UIView * mainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 2*WIDTH, HIGHT-49)];
    _mainView = mainView;
    [self.view addSubview:mainView];
    
    [self setUpNavBar];
    
    [self loadCategory_NameData];
    
    [self loadCategory_LabelViewData];
}

-(void)loadCategory_NameData
{
    
    [CategoryPageTool categoryNameDataWithSuccess:^(NSArray *data) {
        
        self.category_NameArray = data;
        
        self.categoryView.category_names = data;
        
    } failure:^(NSError *error) {
//        NSLog(@"categoryNameData:%@",error);
    }];
}

-(void)loadCategory_GoodsDataWithCat_Id:(NSString *)cat_id
{
    [CategoryPageTool categoryGoodsDataWithCat_Id:cat_id Success:^(NSArray *data) {
        
        self.category_GoodsArray = data;
        
        self.categoryView.category_goods = data;
        
    } failure:^(NSError *error) {
        
    }];
    
}

-(void)loadCategory_LabelViewData
{
    [CategoryPageTool categoryLabelDataWithSuccess:^(NSArray *data) {
       
        self.categoryLabelView.category_labels = data;
        [self.categoryLabelView.collectionView reloadData];
        
    } failure:^(NSError *error) {
        
    }];
    
}


#pragma mark CategoryView代理方法
/**
 *  点击分类名称后加载商品列表数据
 */
-(void)categoryNameViewDidClick:(CategoryView *)categoryView categoryName:(Category_Name *)categoryName index:(NSInteger)index
{
    [self loadCategory_GoodsDataWithCat_Id:categoryName.cat_id];
}

/**
 *  商品列表点击后代理
 */
-(void)categoryGoodsViewDidClick:(CategoryView *)categoryView categoryGoods:(Son_Category *)goods categoryCase:(Category_Case *)category_case index:(NSInteger)index
{
    
    GoodsListController * vc = [[GoodsListController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    if (category_case) {
        if ([category_case.table_name isEqualToString:@"goods"]) {
            
            GoodsController * vc  = [[GoodsController alloc] init];
            vc.good_id = category_case.table_id;
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
        else if ([category_case.table_name isEqualToString:@"key_search"]){
            vc.name_id = category_case.name;
            vc.titleName = category_case.name;
            [self.navigationController pushViewController:vc animated:YES];
        }
        else if ([category_case.table_name isEqualToString:@"goods_category"]){
            vc.category_id = category_case.table_id;
            vc.titleName = category_case.name;
            [self.navigationController pushViewController:vc animated:YES];
        }

    }
        else{
            vc.titleName = goods.name;
            vc.category_id = goods.cat_id;
            [self.navigationController pushViewController:vc animated:YES];
        }
   
//    NSLog(@"%ld--%@--%@",index,goods.name,category_case.table_name);
}


/**
 *  标签列表点击后代理方法
 */
-(void)CategoryLabelViewDidClick:(Category_LabelView *)labelView list:(Label_List *)list index:(NSInteger)index
{
    
    GoodsListController * vc = [[GoodsListController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    vc.label_id = list.label_id;
    vc.titleName = list.name;
    [self.navigationController pushViewController:vc animated:YES];
    
//    NSLog(@"%ld--%@--%@",index,list.name,list.label_id);
}


-(void)setUpNavBar
{
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem barButtonItemWithImage:[UIImage imageNamed:@"ic_search_red"] andHighImage:[UIImage imageNamed:@"ic_search_red"] target:self action:@selector(searchBtn) forControlEvents:UIControlEventTouchUpInside];
    
    UISegmentedControl * segement = [[UISegmentedControl alloc] initWithItems:@[@"分类",@"标签"]];
    self.segement = segement;
    segement.tintColor = [UIColor colorWithRed:0.98f green:0.33f blue:0.34f alpha:1.00f];
    segement.frame = CGRectMake(0, 0, self.view.width*0.55, 25);
    segement.selectedSegmentIndex = 0;
    [segement addTarget:self action:@selector(segeMentClick:) forControlEvents:UIControlEventValueChanged];
    self.navigationItem.titleView = segement;
    
}

-(void)segeMentClick:(UISegmentedControl *)segement
{
    if (segement.selectedSegmentIndex==1) {
        [UIView animateWithDuration:DurTime animations:^{
            self.mainView.transform = CGAffineTransformMakeTranslation(-WIDTH, 0);
        }];
        
    }
    
    else{
        [UIView animateWithDuration:DurTime animations:^{
            self.mainView.transform = CGAffineTransformIdentity;
        }];
        }
}

-(void)searchBtn
{
//    NSLog(@"search");
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    if (self.tabBarController.selectedIndex!=1) {
        self.segement.selectedSegmentIndex = 0;
        self.mainView.transform = CGAffineTransformIdentity;
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
